package com.klw.business.service.impl;

import com.klw.business.domain.*;
import com.klw.business.mapper.*;
import com.klw.business.service.IDataCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 数据中心 ServiceImpl
 */

@Service
public class IDataCenterServiceImpl implements IDataCenterService {

    @Autowired
    private KlwStudentsMapper studentsMapper;

    @Autowired
    private KlwWarningResultsMapper klwWarningResultsMapper;

    @Autowired
    private KlwMlFeaturesMapper klwMlFeaturesMapper;

    @Autowired
    private KlwSemesterMapper klwSemesterMapper;

    @Autowired
    private KlwMajorMapper klwMajorMapper;

    /**
     * 获取基础卡片数据
     *
     * @return 结果
     */
    @Override
    public HashMap<String, Object> getBaseCardData() {
        HashMap<String, Object> resultMap = new HashMap<>();
        // 获取学生总数
        Long totalStudentCount = studentsMapper.getStudentsTotalCount();
        // 获取高预警人数
        Long highRiskCount = klwWarningResultsMapper.getHighRiskCount();
        // 获取预警总数
        Long totalRiskCount = klwWarningResultsMapper.getTotalRiskCount();
        // 获取模型置信度
        BigDecimal modelAvailablePercent = klwWarningResultsMapper.getModelAvailablePercent();
        resultMap.put("total_student_num", totalStudentCount);
        resultMap.put("high_risk_num", highRiskCount);
        resultMap.put("total_risk_num", totalRiskCount);
        resultMap.put("model_available_percent", modelAvailablePercent);
        return resultMap;
    }

    /**
     * 获取折线图数据
     *
     * @param param
     * @return
     */
    @Override
    public HashMap<String, Object> getTrendData(QueryDataParam param) {
        HashMap<String, Object> data = new HashMap<>();
        // 获取学期数据
        List<LineXAxisData> xAxisData = klwSemesterMapper.getSemesterXAxisData(param);
        List<LineYAxisData> yAxisData = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            LineYAxisData yAxisData1 = new LineYAxisData();
            List<TrendRespItem> itemList = new ArrayList<>();
            for (LineXAxisData item : xAxisData) {
                TrendRespItem seriesItem = klwWarningResultsMapper.getRiskNumAndModelAvailablePercent(item.getSemesterId(), i);
                itemList.add(seriesItem);
            }
            yAxisData1.setyAxisData(itemList);
            yAxisData.add(yAxisData1);
        }
        data.put("x_data", xAxisData);
        data.put("y_data", yAxisData);
        return data;
    }

    /**
     * 获取雷达图
     *
     * @param param
     * @return
     */
    @Override
    public RadarData getRadarData(QueryDataParam param) {
        return klwMlFeaturesMapper.getRadarData(param);
    }

    /**
     * 获取饼图
     *
     * @param param
     * @return
     */
    @Override
    public HashMap<String, Object> getPieData(QueryDataParam param) {
        HashMap<String, Object> data = new HashMap<>();
        List<PieData> dataList = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            QueryDataParam item = new QueryDataParam();
            item.setSemesterIds(param.getSemesterIds());
            item.setWarningLevel(i);
            PieData pieItem = klwWarningResultsMapper.getPieData(item);
            dataList.add(pieItem);
        }
        data.put("pie_data", dataList);
        return data;
    }

    /**
     * 获取柱状图
     *
     * @param param
     * @return
     */
    @Override
    public Map<String, List<List<BarData>>> getBarData(QueryDataParam param) {
        HashMap<String, List<List<BarData>>> data = new HashMap<>();
        // 获取专业数据
        List<KlwMajor> majorList = klwMajorMapper.selectKlwMajorList(null);
        List<List<BarData>> dataList = new ArrayList<>();
        for (int i = 0; i < 4; i++){
            ArrayList<BarData> itemList = new ArrayList<>();
            for (KlwMajor item : majorList){
                QueryDataParam qp = new QueryDataParam();
                qp.setSemesterIds(param.getSemesterIds());
                qp.setWarningLevel(i);
                qp.setMarjorId(item.getMajorId());
                BarData barData = klwWarningResultsMapper.getBarData(qp);
                itemList.add(barData);
            }
            dataList.add(itemList);
        }
        data.put("bar_data",dataList);
        return data;
    }
}
